/**
 * 滚动性能优化工具
 * 解决非被动事件监听器警告
 */

// 优化滚动事件监听器
export function optimizeScrollListeners() {
  // 保存原始的 addEventListener 方法
  const originalAddEventListener = EventTarget.prototype.addEventListener
  
  // 重写 addEventListener 方法
  EventTarget.prototype.addEventListener = function(
    type: string,
    listener: EventListenerOrEventListenerObject,
    options?: boolean | AddEventListenerOptions
  ) {
    // 为滚动相关事件自动添加 passive: true
    if (type === 'mousewheel' || type === 'wheel' || type === 'scroll') {
      if (options === undefined) {
        options = { passive: true }
      } else if (typeof options === 'object') {
        options = { ...options, passive: true }
      } else {
        options = { passive: true, capture: options }
      }
    }
    
    return originalAddEventListener.call(this, type, listener, options)
  }
}

// 为特定元素添加滚动优化
export function optimizeElementScroll(element: HTMLElement) {
  if (!element) return
  
  // 添加滚动优化CSS类
  element.classList.add('scroll-optimized')
  
  // 设置标准滚动行为
  element.style.scrollBehavior = 'smooth'
}

// 为所有表格添加滚动优化
export function optimizeTableScroll() {
  const tables = document.querySelectorAll('.el-table__body-wrapper')
  tables.forEach(table => {
    if (table instanceof HTMLElement) {
      optimizeElementScroll(table)
    }
  })
}

// 为所有对话框添加滚动优化
export function optimizeDialogScroll() {
  const dialogs = document.querySelectorAll('.el-dialog__body')
  dialogs.forEach(dialog => {
    if (dialog instanceof HTMLElement) {
      optimizeElementScroll(dialog)
    }
  })
}

// 为所有抽屉添加滚动优化
export function optimizeDrawerScroll() {
  const drawers = document.querySelectorAll('.el-drawer__body')
  drawers.forEach(drawer => {
    if (drawer instanceof HTMLElement) {
      optimizeElementScroll(drawer)
    }
  })
}

// 初始化所有滚动优化
export function initScrollOptimization() {
  // 优化事件监听器
  optimizeScrollListeners()
  
  // 等待 DOM 加载完成后优化元素
  if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', () => {
      optimizeTableScroll()
      optimizeDialogScroll()
      optimizeDrawerScroll()
    })
  } else {
    optimizeTableScroll()
    optimizeDialogScroll()
    optimizeDrawerScroll()
  }
  
  // 监听动态添加的元素
  const observer = new MutationObserver((mutations) => {
    mutations.forEach((mutation) => {
      mutation.addedNodes.forEach((node) => {
        if (node instanceof HTMLElement) {
          // 检查新添加的元素是否是表格、对话框或抽屉
          const tables = node.querySelectorAll('.el-table__body-wrapper')
          const dialogs = node.querySelectorAll('.el-dialog__body')
          const drawers = node.querySelectorAll('.el-drawer__body')
          
          tables.forEach(table => optimizeElementScroll(table as HTMLElement))
          dialogs.forEach(dialog => optimizeElementScroll(dialog as HTMLElement))
          drawers.forEach(drawer => optimizeElementScroll(drawer as HTMLElement))
          
          // 如果节点本身是这些元素
          if (node.classList.contains('el-table__body-wrapper') ||
              node.classList.contains('el-dialog__body') ||
              node.classList.contains('el-drawer__body')) {
            optimizeElementScroll(node)
          }
        }
      })
    })
  })
  
  // 开始观察
  observer.observe(document.body, {
    childList: true,
    subtree: true
  })
}

// 导出默认函数
export default {
  optimizeScrollListeners,
  optimizeElementScroll,
  optimizeTableScroll,
  optimizeDialogScroll,
  optimizeDrawerScroll,
  initScrollOptimization
} 